package com.sinodata.bsm.cicp.collector.sybase;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * OpenConf.java
 * <p>
 * 
 * @author 管普贵 guanpg@uni-mon.com
 * @version 2.0
 * @Date Jan 8, 2010
 */
public class ServerMemory extends AbstractJDBCCollector {

    private static final Logger logger = Logger.getLogger(ServerMemory.class);

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            String[] propMetas = getPropMeta();
            //        String serverName = this.getParamMap().get("服务器名");
            String serverName = this.getParamMap().get("ServerName");

            String sql1 = "select version=convert(float,substring(@@version, charindex('/',@@version)+1, 4))";
            String sql2 = "select config, memory_used, unit from master..syscurconfigs where config in (104, 132, 108)";
            String sql3 = "select config, memory_used, unit from master..syscurconfigs where config in (402, 132, 146)";

            String[] rs = this.getStrings(serverName, sql1, sql2, sql3, ip);
            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
            }

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception ex) {
            logger.error("failed to collect sybase server memory", ex);
            throw new CollectorException(ex);
        }
        return values;
    }

    private String[] getStrings(String serverName, String sql1, String sql2, String sql3, String ip) throws CollectorException {
        String[] propValArr = new String[3];
        try {
            ResultSet rst = this.getCachedResultSet("sybase", ip, serverName, sql1);
            float version = 0;
            if (rst.next()) {
                version = rst.getFloat(1);
            }
            if (rst != null) {
                rst.close();
                rst = null;
            }
            if (version < 12.5)
                rst = this.getCachedResultSet("sybase", ip, serverName, sql2);
            else
                rst = this.getCachedResultSet("sybase", ip, serverName, sql3);

            Map<String, Integer> map = new HashMap<String, Integer>();
            while (rst.next()) {
                String config = getString(rst.getString(1));
                int memory_used = rst.getInt(2);
                String unit = getString(rst.getString(3));
                int value = (unit.equals("memory pages(2k)") ? memory_used / 1024 : memory_used * 2 / 1024);
                map.put(config, value);
            }
            propValArr[0] = "" + (version < 12.5 ? map.get("104") : map.get("402"));
            propValArr[1] = "" + map.get("132");
            propValArr[2] = "" + (version < 12.5 ? map.get("108") : map.get("146"));

        } catch (Exception ex) {
            logger.error("while connect database : ", ex);
            throw new CollectorException(ex);
        }
        return propValArr;
    }

    public String[] getParamMeta() {
        return new String[] { "ServerName" };
    }

    public String[] getPropMeta() {
        //        return new String[] { "内存大小", "数据缓存大小", "程序缓存大小" };
        return new String[] { "MemorySize", "DataCacheSize", "ProgramCacheSize" };
    }

    private String getString(String str) {
        return str == null ? "" : str.trim();
    }
}
